package com.ol.edu.server.controller.Authority;


import com.ol.edu.server.entity.*;
import com.ol.edu.server.service.Authority.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Roger
 * @date 2020/9/8 20:36
 * @description
 */
@RestController
@RequestMapping("/role")
@CrossOrigin
public class RoleController {

    @Autowired
    private RoleService roleService;
    /*
        查询菜单列表
     */
    @RequestMapping("/findAllRole")
    public ResponseResult findAllRole(@RequestBody Role role) {

        List<Role> roleList = roleService.findAllRole(role);
        return new ResponseResult(true, 200, "查询所有角色成功", roleList);
    }
    /*
        添加&修改角色
     */
    @RequestMapping("/saveOrUpdateRole")
    public ResponseResult saveOrUpdateRole(@RequestBody Role role) {
        //如果传入的ID为空表示是新增
        if (role.getId() == null) {
            roleService.saveRole(role);
            return new ResponseResult(true, 200, "添加角色成功", null);
        } else {//更新
            roleService.updateRole(role);
            return new ResponseResult(true, 200, "修改角色成功", null);
        }
    }
    /*
        查询所有菜单列表
     */
    @RequestMapping("/findAllMenu")
    public ResponseResult findAllMenu() {
        List<Menu> menuList = roleService.findAllMenu();
        Map map = new HashMap(1);
        map.put("parentMenuList", menuList);
        return new ResponseResult(true, 200, "查询所有菜单列表成功", map);
    }
    /*
        根据角色信息查询关联菜单
     */
    @RequestMapping("/findMenuByRoleId")
    public ResponseResult findMenuByRoleId(Integer roleId) {
        List<Integer> integerList = roleService.findMenuByRoleId(roleId);
        return new ResponseResult(true, 200, "根据角色信息查询关联菜单成功", integerList);
    }
    /*
        为角色分配菜单列表
     */
    @RequestMapping("/RoleContextMenu")
    public ResponseResult roleContextMenu(@RequestBody RoleMenuVo roleMenuVo) {
        roleService.roleContextMenu(roleMenuVo);
        return new ResponseResult(true, 200, "为角色分配菜单列表成功", null);
    }
    /*
        删除角色
     */
    @RequestMapping("/deleteRole")
    public ResponseResult deleteRole(Integer id) {
        roleService.deleteRole(id);
        return new ResponseResult(true, 200, "删除角色成功", null);

    }
    /*
        获取当前角色拥有的 资源信息
     */
    @RequestMapping("/findResourceListByRoleId")
    public ResponseResult findResourceListByRoleId(Integer roleId) {
        List<ResourceCategory> resourceCategoryList = roleService.findResourceListByRoleId(roleId);
        return new ResponseResult(true, 200, "获取当前角色拥有的 资源信息成功", resourceCategoryList);
    }
    /*
        为角色分配菜单
     */
    @RequestMapping("/roleContextResource")
    public ResponseResult roleContextResource(@RequestBody RoleResourceVo roleResourceVo) {
        roleService.roleContextResource(roleResourceVo);
        return new ResponseResult(true, 200, "角色分配菜单成功", null);
    }

}
